package com.xiaoma.tpo.webservice;

import com.xiaoma.tpo.domain.User;
import com.xiaoma.tpo.dto.UserDto;
import com.xiaoma.tpo.dto.UserTagDto;
import com.xiaoma.tpo.service.TagsService;
import com.xiaoma.tpo.service.UserService;
import com.xiaoma.tpo.utils.TPOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Owner on 2015/5/6.
 */
@Controller
@RequestMapping("/service/userTag")
public class TagWs {
    @Autowired
    private UserService userService;

    @Autowired
    private TagsService tagsService;

    @RequestMapping(value = "/set", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> setUserTag(@RequestHeader String token, @RequestBody UserTagDto dto){
        Map<String, Object> result = new HashMap<String, Object>();
        User user = userService.findByToken(token);
        if(null == user) {
            result.put("status", -2);
            result.put("message", "invalid token");
            return result;
        }
        if (TPOUtils.isAnonymous(user)) {
            result.put("status", 0);
            result.put("message", "anonymous can't access");
            return result;

        }
        if (TPOUtils.over7days(new Date(), user.getLastVisit())) {//大于7天token失效
            result.put("status", -1);
            result.put("message", "token disabled over 7 days, please login");
            return result;
        }

        tagsService.setTag(user,dto.getTagname());
        result.put("status", 1);
        return result;
    }
}
